package cn.tedu.crm.notice.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.tedu.crm.base.pojo.PageObject;
import cn.tedu.crm.base.response.JsonResult;
import cn.tedu.crm.notice.pojo.dto.NoticeQuery;
import cn.tedu.crm.notice.pojo.dto.NoticeSaveParam;
import cn.tedu.crm.notice.pojo.vo.NoticeVO;
import cn.tedu.crm.notice.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "公告模块")
@RestController
@RequestMapping("/api/crm/notice/")
@Slf4j
public class NoticeController {
    @Autowired
    NoticeService noticeService;

    @ApiOperation("查询当前用户的公告列表")
    @GetMapping("select")
    public JsonResult select(NoticeQuery noticeQuery){
        log.debug("查询当前用户的公告列表:入参{}",noticeQuery);
        PageObject<NoticeVO> pageObject = PageObject.page(
                noticeQuery.getPageNum(), noticeQuery.getPageSize(), () -> noticeService.loadNotice(noticeQuery));
        log.debug("查询当前用户的公告列表:出参{}",pageObject);
        return JsonResult.success(pageObject);
    }
    @ApiOperation("根据id查询公告")
    @GetMapping("selectById")
    public JsonResult selectByIdAndUserId( Long id){
        log.debug("根据id和用户id查询公告:id:{}",id);
        List<NoticeVO> list = noticeService.loadNoticeByIdAndUserId(id);
        log.debug("根据id和用户id查询公告:出参{}",list);
        return JsonResult.success(list);
    }
    @ApiOperation("保存公告")
    @PostMapping("save")
    public JsonResult save(@RequestBody NoticeSaveParam noticeSaveParam){
        log.debug("保存公告:入参{}",noticeSaveParam);
        noticeService.saveNotice(noticeSaveParam);
        return JsonResult.ok();
    }
    @ApiOperation("删除公告")
    @PostMapping("delete")
    public JsonResult deleteById(Long id){
        log.debug("删除公告:入参{}",id);
        noticeService.deleteNoticeById(id);
        return JsonResult.ok();
    }
    @ApiOperation("查询当前用户的未读公告")
    @GetMapping("selectByStatus")
    public JsonResult selectByStatus(NoticeQuery noticeQuery){
        log.debug("查询当前用户的未读公告列表:入参{}",noticeQuery);
        PageObject<NoticeVO> pageObject = PageObject.page(
                noticeQuery.getPageNum(), noticeQuery.getPageSize(), () -> noticeService.selectByStatus(noticeQuery));
        log.debug("查询当前用户的未读公告列表:出参{}",pageObject);
        return JsonResult.success(pageObject);
    }
    @ApiOperation("查询未读公告数量")
    @GetMapping("count")
    public JsonResult selectNoticeCount(NoticeQuery noticeQuery){
        log.debug("查询未读公告数量:入参{}",noticeQuery);
        Long count = noticeService.selectNoticeCount(noticeQuery);
        log.debug("查询未读公告数量:出参{}",count);
        return JsonResult.success(count);
    }

}
